package datastructure.stack;


/**
 * @author jack.wu
 * @version 1.0
 * @date 2020-03-21
 */
public class ArrayStackDemo {

}

class ArrayStack {
    private int maxSize = 100;
    private int top = -1;
    private int[] stack;

    public ArrayStack(int maxSize) {
        if (maxSize < 0) {
            throw new RuntimeException("初始化长度参数错误");
        }
        if (maxSize > this.maxSize) {
            stack = new int[this.maxSize];
        } else {
            stack = new int[maxSize];
        }
    }

    /**
     * 入栈
     *
     * @param n 入栈数
     */
    public void push(int n) {
        if (isFull()) {
            System.out.println("栈满~~~~");
            return;
        }
        top++;
        stack[top] = n;
    }

    /**
     * 出栈
     *
     * @return 出栈数
     */
    public int pop() {
        if (isEmpty()) {
            throw new RuntimeException("空栈~~~~");
        }
        int val = stack[top];
        top--;
        return val;
    }

    /**
     * 判断栈是否是空栈
     */
    public boolean isEmpty() {
        return top == -1;
    }

    /**
     * 判断栈满
     */
    public boolean isFull() {
        return top == maxSize - 1;
    }

    /**
     * 判断是否是运算符
     *
     * @param val 运算符
     */
    public boolean isOper(char val) {
        return val == '+' || val == '-' || val == '*' || val == '/';
    }

    /**
     * 计算
     */
    public int cal(int num1, int num2, int oper) {
        int res = 0;
        switch (oper) {
            case '+':
                res = num2 + num1;
                break;
            case '-':
                res = num2 - num1;
                break;
            case '*':
                res = num2 * num1;
                break;
            case '/':
                res = num2 / num1;
                break;
            default:
                break;
        }
        return res;
    }

    /**
     * 遍历
     */
    public void showStack() {
        if (isEmpty()) {
            System.out.println("空栈~~~~");
            return;
        }
        for (int i = top; top > -1; top--) {
            System.out.println(stack[i]);
        }
    }
}